﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Microsoft.Practices.Unity;
using MessageContracts = Skynet.ServiceContracts.MessageContracts;
using BusinessComponent = Skynet.BusinessLogic.BusinessComponents;
using BusinesEntities = Skynet.BusinessLogic;
using System.Configuration;
using Microsoft.Practices.Unity.Configuration;
using Infrastructure.Aspect.Extensions;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using System.ServiceModel.DomainServices.Server;
//<%@ ServiceHost Service="Skynet.ServiceImplementation.SkynetDomainService" Factory="System.ServiceModel.DomainServices.Hosting.DomainServiceHostFactory, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"%>
namespace SkynetWebServices
{
    public class DomainServiceHostFactory : System.ServiceModel.DomainServices.Hosting.DomainServiceHostFactory
    {
        public IUnityContainer ContainerService { get; set; }
        public DomainServiceHostFactory()
        {
            try
            {
                this.ContainerService = System.Web.HttpContext.Current.Application["ContainerService"] as IUnityContainer;                                               

            }
            catch (Exception ex)
            {
                
                throw;
            }
        }
        protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {

            DomainService.Factory = new DomainUnityFactory(this.ContainerService); 
            DomainUnityServiceHost host = null;         
            host = new DomainUnityServiceHost(serviceType, baseAddresses);
            host.Container = this.ContainerService;
            DomainServiceBehavior behavior = new DomainServiceBehavior();            
            behavior.InstanceProvider.Container = ContainerService;            
            behavior.AddToHost(host);
            return host;
        }    
    }
    public class DomainUnityFactory : System.ServiceModel.DomainServices.Server.IDomainServiceFactory
    {
        private IUnityContainer container;
        public DomainUnityFactory(IUnityContainer container) { this.container = container; }
        public DomainService CreateDomainService(Type domainServiceType, DomainServiceContext context)
        {
            var domain = this.container.Resolve(domainServiceType) as DomainService;
            domain.Initialize(context);
            return domain;
        }
        public void ReleaseDomainService(DomainService domainService)
        {
            domainService.Dispose();
        }
    }

    public class DomainUnityServiceHost : System.ServiceModel.DomainServices.Hosting.DomainServiceHost
    {

        public IUnityContainer Container { set; get; } 
        public DomainUnityServiceHost(Type type, Uri[] baseAddresses):base(type, baseAddresses)
        {          
        
              
        }

        void DomainUnityServiceHost_Opening(object sender, EventArgs e)
        {
            
        }
        protected override void OnOpening()
        {            
            base.OnOpening();
        }
        protected override void AddDefaultBehaviors()
        {   
            base.AddDefaultBehaviors();
        }
    }
}